package croleta.core;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import croleta.core.file.InfoFile;

/**
 * <b><code>PathManager</code></b><br><br>
 * This Class Object is responsible for analyzing the URIs, and directing them to the relevant object.
 * @author Eyal Shalev
 */
public class PathManager extends HttpServlet {
	private static final long serialVersionUID = -7158622428554789380L;

	/**
	 * <b>getURI:</b><br>
	 * @return the real requested path URI (including the server).
	 */
	public String getURI() {return null;}
	
	/**
	 * <b>doGet:</b><br>
	 * This function is called for all given URIs.<br>
	 * It analyzes the path, and breaks it to pars.<br>
	 * Then it <code>forwards</code> it to the active theme to create the page.<br>
	 * @throws ServletException
	 * @param req {@link HttpServletRequest}
	 * @param resp {@link HttpServletResponse}
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException {
		InfoFile info = new InfoFile("/info.json", getServletContext());
		try {
			req.getRequestDispatcher("/modules/"+info.getTheme()+"/page.jsp").forward(req, resp);
		} catch (IOException e) { throw new ServletException(e.getCause());
		}
	}

	/**
	 * <b>doPost:</b><br>
	 * This function is called for all given URIs.<br>
	 * It analyzes the path, and breaks it to pars.<br>
	 * Then it <code>forwards</code> it to the active theme to create the page.<br><br>
	 * <i><b>Note:</b> This function only forwards the parameters to PathManager.doGet.</i>
	 * @throws ServletException
	 * @throws IOException
	 * @param req {@link HttpServletRequest}
	 * @param resp {@link HttpServletResponse}
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {doGet(req, resp);}
}
